package com.daniel.cms.service;

import com.daniel.cms.config.UploadConfig;
import com.daniel.cms.entity.AttachEntity;
import com.daniel.cms.mapper.AttachMapper;
import com.daniel.cms.utils.Mes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/4/28 09:50
 */
@Service
public class AttachServer {

	@Autowired
	AttachMapper mapper;
	@Autowired
	UploadConfig uploadConfig;

	public Mes upload(MultipartFile file, String key) {
		try {
			AttachEntity entity = init(file, key);
			if (entity != null) {
				int result = mapper.insert(entity);
				return result > 0 ? Mes.success(entity.getPath()) : Mes.error("上传失败");
			} else {
				return Mes.error("文件为空，请选择文件");
			}
		} catch (IOException e) {
			return Mes.error("上传失败：" + e.getMessage());
		}
	}

	public Mes multiUpload(HttpServletRequest request) {
		try {
			List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
			String key = request.getParameter("key");
			String filePath = uploadConfig.getPath();
			for (int i = 0; i < files.size(); i++) {
				MultipartFile file = files.get(i);
				if (file.isEmpty()) {
					return Mes.error("上传的文件有空文件");
				}
				String fileName = file.getOriginalFilename();
				String suffixName = fileName.substring(fileName.lastIndexOf("."));
				Long size = file.getSize();
				File dest = new File(filePath + UUID.randomUUID() + suffixName);
				if (!dest.getParentFile().exists()) {
					dest.getParentFile().mkdirs();
				}
				file.transferTo(dest);
				AttachEntity entity = new AttachEntity();
				entity.setName(fileName)
						.setSid(key)
						.setSuffix(suffixName)
						.setSize(size)
						.setPath(filePath);
				int result = mapper.insert(entity);
				if (result <= 0) {
					return Mes.error("上传失败");
				}
			}
			return Mes.success();
		} catch (Exception e) {
			return Mes.error("上传失败：" + e.getMessage());
		}
	}

	private AttachEntity init(MultipartFile file, String key) throws IOException {
		if (!file.isEmpty()) {
			String fileName = file.getOriginalFilename();
			String suffixName = fileName.substring(fileName.lastIndexOf("."));
			Long size = file.getSize();
			String filePath = uploadConfig.getPath() + File.separator;
			Date time = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			sdf.format(time);
			String newName = sdf.format(time) + File.separator + UUID.randomUUID() + suffixName;
			File dest = new File(filePath + newName);
			if (!dest.getParentFile().exists()) {
				dest.getParentFile().mkdirs();
			}
			file.transferTo(dest);
			String path = uploadConfig.getUrl() + newName;
			path = path.replace("\\", "/");
			AttachEntity entity = new AttachEntity();
			entity.setName(fileName)
					.setSid(key)
					.setSuffix(suffixName)
					.setSize(size)
					.setPath(path);
			return entity;
		}
		return null;
	}
}
